package com.xl333.util;

import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;

/**
 * @auth liux
 * @date 2021/6/18
 */
public class PropertiesUtil {

    private static ResourceBundle resourceBundle;

    private static void initResourceBundle(){
        //classes下的文件路径，不需要后缀名
        resourceBundle = ResourceBundle.getBundle("application");
    }

    /**
     * 获取属性配置文件值
     * @param key
     * @return
     */
    public static String getValue(String key) {
        if(resourceBundle == null){
            initResourceBundle();
        }

        return resourceBundle.getString(key);
    }

    /**
     * 获取属性配置文件中文值
     * @param key
     * @return
     * @throws UnsupportedEncodingException
     */
    public static String getChineseValue(String key) throws UnsupportedEncodingException {
        return decodeUTF8(getValue(key));
    }

    private static String decodeUTF8(String str) throws UnsupportedEncodingException {
        return new String(str.getBytes("ISO-8859-1"),"UTF-8");
    }

}
